package flc.ast.fragment;

import android.os.Bundle;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.LinearLayoutManager;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.footer.ClassicsFooter;
import com.scwang.smartrefresh.layout.header.ClassicsHeader;
import com.scwang.smartrefresh.layout.listener.OnRefreshLoadMoreListener;

import java.util.ArrayList;
import java.util.List;

import flc.ast.AppConfig;
import flc.ast.R;
import flc.ast.adapter.CircleAdapter;
import flc.ast.adapter.ShareAdapter;
import flc.ast.adapter.TagAdapter;
import flc.ast.databinding.FragmentCommunityBinding;
import flc.ast.utils.AppSpUtils;
import stark.common.api.StkResApi;
import stark.common.base.IReqRetCallback;
import stark.common.basic.base.BaseNoModelFragment;
import stark.common.basic.base.BaseWebviewActivity;
import stark.common.basic.event.EventStatProxy;
import stark.common.basic.utils.IntentUtil;
import stark.common.bean.StkResBean;

/**
 * A simple {@link Fragment} subclass.
 * Use the {@link CommunityFragment#newInstance} factory method to
 * create an instance of this fragment.
 */
public class CommunityFragment extends BaseNoModelFragment<FragmentCommunityBinding> {

    private TagAdapter tagAdapter;
    private ShareAdapter shareAdapter;
    private CircleAdapter circleAdapter;
    private int page1 = 1;
    private int page2 = 1;
    private int refreshTime = 200;

    @Override
    protected int onCreate() {
        return R.layout.fragment_community;
    }

    @Override
    protected void initView() {
        EventStatProxy.getInstance().statEvent1(getActivity(), mDataBinding.event1Container);
        EventStatProxy.getInstance().statEvent5(getActivity(), mDataBinding.event5Container);

        mDataBinding.tvDiscover.setOnClickListener(this);
        mDataBinding.tvCircle.setOnClickListener(this);

        mDataBinding.rvTagList.setLayoutManager(new LinearLayoutManager(mContext));
        tagAdapter = new TagAdapter();
        mDataBinding.rvTagList.setAdapter(tagAdapter);
        tagAdapter.setOnItemClickListener(this);

        mDataBinding.rvShareList.setLayoutManager(new GridLayoutManager(mContext, 2));
        shareAdapter = new ShareAdapter();
        mDataBinding.rvShareList.setAdapter(shareAdapter);
        shareAdapter.setOnItemClickListener(this);

        mDataBinding.refreshShareItem.setRefreshHeader(new ClassicsHeader(mContext));
        mDataBinding.refreshShareItem.setRefreshFooter(new ClassicsFooter(mContext));
        mDataBinding.refreshShareItem.setOnRefreshLoadMoreListener(new OnRefreshLoadMoreListener() {
            @Override
            public void onLoadMore(@NonNull RefreshLayout refreshLayout) {
                page1++;
                getShareData();
                mDataBinding.refreshShareItem.finishLoadMore(refreshTime);
            }

            @Override
            public void onRefresh(@NonNull RefreshLayout refreshLayout) {
                page1 = 1;
                getShareData();
                mDataBinding.refreshShareItem.finishRefresh(refreshTime);
            }
        });

        mDataBinding.rvCircleList.setLayoutManager(new LinearLayoutManager(mContext));
        circleAdapter = new CircleAdapter();
        mDataBinding.rvCircleList.setAdapter(circleAdapter);
        circleAdapter.addChildClickViewIds(R.id.ivCircleItemShare);
        circleAdapter.addChildClickViewIds(R.id.ivCircleItemLike);
        circleAdapter.setOnItemClickListener(this);
        circleAdapter.setOnItemChildClickListener(this);

        mDataBinding.refreshCircleItem.setRefreshHeader(new ClassicsHeader(mContext));
        mDataBinding.refreshCircleItem.setRefreshFooter(new ClassicsFooter(mContext));
        mDataBinding.refreshCircleItem.setOnRefreshLoadMoreListener(new OnRefreshLoadMoreListener() {
            @Override
            public void onLoadMore(@NonNull RefreshLayout refreshLayout) {
                page2++;
                getCircleData();
                mDataBinding.refreshCircleItem.finishLoadMore(refreshTime);
            }

            @Override
            public void onRefresh(@NonNull RefreshLayout refreshLayout) {
                page2 = 1;
                getCircleData();
                mDataBinding.refreshCircleItem.finishRefresh(refreshTime);
            }
        });
    }

    @Override
    protected void onClickCallback(View v) {
        switch (v.getId()) {
            case R.id.tvDiscover://发现
                clearSelection();
                mDataBinding.llDiscover.setVisibility(View.VISIBLE);
                mDataBinding.llDiscoverContent.setVisibility(View.VISIBLE);
                break;
            case R.id.tvCircle://圈子
                clearSelection();
                mDataBinding.llCircle.setVisibility(View.VISIBLE);
                mDataBinding.llCircleContent.setVisibility(View.VISIBLE);
                break;
        }
    }

    @Override
    protected void onItemClickCallback(@NonNull BaseQuickAdapter<?, ?> adapter, @NonNull View view, int position) {
        StkResBean bean = (StkResBean) adapter.getItem(position);
        BaseWebviewActivity.open(mContext, bean.getUrl(), bean.getName());
    }

    @Override
    public void onItemChildClick(@NonNull BaseQuickAdapter adapter, @NonNull View view, int position) {
        switch (view.getId()) {
            case R.id.ivCircleItemShare://分享
                IntentUtil.shareText(mContext, circleAdapter.getItem(position).getUrl());
                break;
            case R.id.ivCircleItemLike://收藏
                List<StkResBean> listSp = AppSpUtils.getCollectList();
                View itemView = mDataBinding.rvCircleList.getChildAt(position);
                TextView textView = itemView.findViewById(R.id.tvCircleItemLike);
                ImageView imageView = itemView.findViewById(R.id.ivCircleItemLike);
                int num = Integer.parseInt(textView.getText().toString().substring(0, 4));
                if (circleAdapter.getItem(position).isSelected()) {
                    listSp.remove(circleAdapter.getItem(position));
                    AppSpUtils.saveCollectList(listSp);
                    circleAdapter.getItem(position).setSelected(false);
                    num--;
                } else {
                    List<StkResBean> listSave = new ArrayList<>();
                    circleAdapter.getItem(position).setSelected(true);
                    listSave.add(circleAdapter.getItem(position));
                    if (listSp != null && listSp.size() != 0) {
                        listSp.addAll(listSave);
                        AppSpUtils.saveCollectList(listSp);
                    } else {
                        AppSpUtils.saveCollectList(listSave);
                    }
                    num++;
                }
                textView.setText(getString(R.string.collect_num, num));
                if (circleAdapter.getItem(position).isSelected()) {
                    imageView.setImageResource(R.drawable.ayidianzan);
                } else {
                    imageView.setImageResource(R.drawable.adianzan);
                }
                break;
        }
    }

    @Override
    protected void initData() {
        getTagData();
        getShareData();
        getCircleData();
    }

    private void getTagData() {
        StkResApi.getTagResourceList(this, AppConfig.TAG_DATA, StkResApi.createParamMap(0, 3), false, new IReqRetCallback<List<StkResBean>>() {
            @Override
            public void onResult(boolean success, String msg, @Nullable List<StkResBean> result) {
                if (success) {
                    if (result != null && result.size() > 0) {
                        tagAdapter.setList(result);
                    }
                }
            }
        });
    }

    private void getShareData() {
        StkResApi.getTagResourceList(this, AppConfig.SHARE_DATA, StkResApi.createParamMap(0, 10), false, new IReqRetCallback<List<StkResBean>>() {
            @Override
            public void onResult(boolean success, String msg, @Nullable List<StkResBean> result) {
                if (success) {
                    if (result != null && result.size() > 0) {
                        if (page1 == 1) {
                            shareAdapter.setList(result);
                        } else {
                            shareAdapter.addData(result);
                        }
                    }
                }
            }
        });
    }

    private void getCircleData() {
        StkResApi.getTagResourceList(this, AppConfig.CIRCLE_DATA, StkResApi.createParamMap(0, 10), false, new IReqRetCallback<List<StkResBean>>() {
            @Override
            public void onResult(boolean success, String msg, @Nullable List<StkResBean> result) {
                if (success) {
                    if (result != null && result.size() > 0) {
                        if (page2 == 1) {
                            circleAdapter.setList(result);
                        } else {
                            circleAdapter.addData(result);
                        }
                    }
                }
            }
        });
    }

    private void clearSelection() {
        mDataBinding.llDiscover.setVisibility(View.GONE);
        mDataBinding.llCircle.setVisibility(View.GONE);
        mDataBinding.llDiscoverContent.setVisibility(View.GONE);
        mDataBinding.llCircleContent.setVisibility(View.GONE);
    }

}